<?php
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006 - 2008 The Bitsand Project (http://bitsand.googlecode.com/)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

include ('../inc/inc_head_db.php');
include ('../inc/inc_admin.php');
include ('../inc/inc_head_html.php');
?>

<h1><?php echo TITLE?> - View Details</h1>

<p><a href = "admin.php">Admin</a></p>

<?php
if ($_GET ['green'] != '')
	echo "<p class = 'green'>" . htmlentities ($_GET ['green']) . "</p>\n";
if ($_GET ['warn'] != '')
	echo "<p class = 'warn'>" . htmlentities ($_GET ['warn']) . "</p>\n";

//Player ID is passed as GET. Should be an integer, so cast to integer
$pid = (int) $_GET ['pid'];
$key = CRYPT_KEY;
$db_prefix = DB_PREFIX;

//Check for confirmed OOC details
if ($_GET ['btnSubmitOOC'] != '' && CheckReferrer ('admin_viewdetails.php')) {
	$sDate = date ('Y-m-d');
	//Check if player already has an entry in bookings table
	$sql = "SELECT * FROM {$db_prefix}bookings WHERE bkPlayerID = $pid";
	$result = ba_db_query ($link, $sql);
	//If player has not booked insert a new row
	if (ba_db_num_rows ($result) == 0) {
		$sql = "INSERT INTO {$db_prefix}bookings (bkPlayerID, bkDateOOCConfirmed) VALUES ($pid, '$sDate')";
		if (! ba_db_query ($link, $sql)) {
			$sWarn = "There was a problem confirming OOC details";
			LogError ("Error inserting new OOC booking (admin).\nPlayer ID: $pid");
		}
	}
	else {
		//Update existing row
		$sql = "UPDATE {$db_prefix}bookings SET bkDateOOCConfirmed = '$sDate' WHERE bkPlayerID = $pid";
		if (! ba_db_query ($link, $sql)) {
			$sWarn = "There was a problem confirming OOC details";
			LogError ("Error inserting new OOC booking (admin).\nPlayer ID: $pid");
		}
	}
}

//Check for confirmed IC details
if ($_GET ['btnSubmitIC'] != '' && CheckReferrer ('admin_viewdetails.php')) {
	$sDate = date ('Y-m-d');
	//Check if player already has an entry in bookings table
	$sql = "SELECT * FROM {$db_prefix}bookings WHERE bkPlayerID = $pid";
	$result = ba_db_query ($link, $sql);
	//If player has not booked insert a new row
	if (ba_db_num_rows ($result) == 0) {
		$sql = "INSERT INTO {$db_prefix}bookings (bkPlayerID, bkDateICConfirmed) VALUES ($pid, '$sDate')";
		if (! ba_db_query ($link, $sql)) {
			$sWarn = "There was a problem confirming IC details";
			LogError ("Error inserting new IC booking (admin).\nPlayer ID: $pid");
		}
	}
	else {
		//Update existing row
		$sql = "UPDATE {$db_prefix}bookings SET bkDateICConfirmed = '$sDate' WHERE bkPlayerID = $pid";
		if (! ba_db_query ($link, $sql)) {
			$sWarn = "There was a problem confirming IC details";
			LogError ("Error inserting new IC booking (admin).\nPlayer ID: $pid");
		}
	}
}

//Check to see if IC and/or OOC details have already been confirmed
$sql = "SELECT * FROM {$db_prefix}bookings WHERE bkPlayerID = $pid";
$result = ba_db_query ($link, $sql);
if (ba_db_num_rows ($result) != 0) {
	$row = ba_db_fetch_assoc ($result);
	$OOC_Confirmed = $row ['bkDateOOCConfirmed'];
	$IC_Confirmed = $row ['bkDateICConfirmed'];
	if ($OOC_Confirmed == '0000-00-00')
		$OOC_Confirmed = '';
	if ($IC_Confirmed == '0000-00-00')
		$IC_Confirmed = '';
}

if ($pid != 0) {
	echo "<h2>Player ID " . PID_PREFIX . sprintf ('%03s', $pid) . "</h2>\n";
	//Get OOC details
	$sql = "SELECT plFirstName, " .
		"plSurname, " .
		"AES_DECRYPT(pleAddress1, '$key') AS dAddress1, " .
		"AES_DECRYPT(pleAddress2, '$key') AS dAddress2, " .
		"AES_DECRYPT(pleAddress3, '$key') AS dAddress3, " .
		"AES_DECRYPT(pleAddress4, '$key') AS dAddress4, " .
		"AES_DECRYPT(plePostcode, '$key') AS dPostcode, " .
		"AES_DECRYPT(pleTelephone, '$key') AS dTelephone, " .
		"AES_DECRYPT(pleMobile, '$key') AS dMobile, " .
		"plEmail, " .
		"plDOB, " .
		"AES_DECRYPT(pleMedicalInfo, '$key') AS dMedicalInfo, " .
		"plEmergencyName, " .
		"AES_DECRYPT(pleEmergencyNumber, '$key') AS dEmergencyNumber, " .
		"plEmergencyRelationship, " .
		"plCarRegistration, " .
		"plDietary, " .
		"plBookAs, " .
		"plBunkRequested, " .
		"plNotes, " .
		"plAdminNotes " .
		"FROM {$db_prefix}players " .
		"WHERE plPlayerID = $pid";
	$result = ba_db_query ($link, $sql);
	$row = ba_db_fetch_assoc ($result);
	
	//Check if all required info is present
	$OOC_OK = True;
	if ($row ['plFirstName'] == '' || $row ['plSurname'] == '')
		$OOC_OK = False;
	if ($row ['dAddress1'] == '' || $row ['plEmergencyName'] == '')
		$OOC_OK = False;
	if ($row ['dEmergencyNumber'] == '' || $row ['plEmergencyRelationship'] == '')
		$OOC_OK = False;
	if ($row ['plCarRegistration'] == 'Enter NA if you do not drive' || $row ['plDietary'] == 'Select one')
		$OOC_OK = False;
	?>
	
	<hr>
	<h3>OOC Details</h3>
	<table>
	<?php
	echo "<tr class = 'highlight'><td>Name:</td><td>" . htmlentities (stripslashes ($row ['plFirstName'])) . " " .
		htmlentities (stripslashes ($row ['plSurname'])) . "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Address:</td><td>";
	echo htmlentities (stripslashes ($row ['dAddress1'])) . "<br>\n";
	if ($row ['dAddress2'] != '')
		echo htmlentities (stripslashes ($row ['dAddress2'])) . "<br>\n";
	if ($row ['dAddress3'] != '')
		echo htmlentities (stripslashes ($row ['dAddress3'])) . "<br>\n";
	if ($row ['dAddress4'] != '')
		echo htmlentities (stripslashes ($row ['dAddress4'])) . "<br>\n";
	echo "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Postcode:</td><td>" . htmlentities (stripslashes ($row ['dPostcode'])) . "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Telephone:</td><td>" . htmlentities (stripslashes ($row ['dTelephone'])) . "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Mobile:</td><td>" . htmlentities (stripslashes ($row ['dMobile'])) . "</td></tr>\n";
	$sEmail = htmlentities (SafeEmail (stripslashes ($row ['plEmail'])));
	echo "<tr class = 'highlight'><td>E-mail:</td><td><a href = 'mailto:$sEmail'>$sEmail</a></td></tr>\n";
	//Date of birth is stored in YYYYMMDD format - need to decode
	$sDoB = $row ['plDOB'];
	$iDobYear = substr ($sDoB, 0, 4);
	$iMonth = substr ($sDoB, 4, 2);
	$iDate = substr ($sDoB, 6, 2);
	echo "<tr class = 'highlight'><td>Date of Birth:</td><td>$iDate-$iMonth-$iDobYear</td></tr>\n";
	echo "<tr class = 'highlight'><td>Medial Info:</td><td>" . htmlentities (stripslashes ($row ['dMedicalInfo'])) . "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Emergency Contact:</td><td>" . htmlentities (stripslashes ($row ['plEmergencyName'])) . "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Emergency Contact (number):</td><td>" .
		htmlentities (stripslashes ($row ['dEmergencyNumber'])) . "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Emergency Contact (relationship):</td><td>" .
		htmlentities (stripslashes ($row ['plEmergencyRelationship'])) . "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Car Registration:</td><td>" . htmlentities (stripslashes ($row ['plCarRegistration'])) . "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Dietary Requirements:</td><td>" . htmlentities (stripslashes ($row ['plDietary'])) . "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Booking As:</td><td>" . htmlentities (stripslashes ($row ['plBookAs'])) . "</td></tr>\n";
	if ($row ['plBunkRequested'] == 0)
		$sBunk = 'No';
	else
		$sBunk = 'Yes';
	echo "<tr class = 'highlight'><td>Bunk Requested:</td><td>$sBunk</td></tr>\n";
	echo "<tr class = 'highlight'><td>General Notes (not medical/allergy):</td>";
	echo "<td>" . htmlentities (stripslashes ($row ['plNotes'] )) . "</td></tr>\n";
	echo "<tr class = 'highlight'><td>Admin Notes (only visible to admins):</td>";
	echo "<td>" . htmlentities (stripslashes ($row ['plAdminNotes'] )) . "</td></tr>\n";
	echo "<tr><td colspan = '2'>&nbsp;</td></tr>\n";
	echo "<tr><td class = 'mid'><a href = 'admin_edit_ooc.php?pid=$pid'>Edit OOC Details</a></td>\n";
	if ($OOC_OK && $OOC_Confirmed == '') {
		echo "<td class = 'mid'>\n";
		echo "<form action = 'admin_viewdetails.php' method = 'get'>\n";
		echo "<input type = 'hidden' value = '$pid' name = 'pid'>\n";
		echo "<input type = 'submit' value = 'Confirm OOC Details' name = 'btnSubmitOOC'>\n</form></td>\n";
	}
	elseif ($OOC_Confirmed == '')
		echo "<td><span class = 'sans-warn'>Cannot confirm - required information missing</span></td>\n";
	else
		echo "<td><span class = 'sans-green'>OOC details confirmed $OOC_Confirmed</span></td>\n";
	echo "</tr>\n";
	?>
	</table>
	</p>
	<hr>
	
	<h3>IC Details</h3>
	<?php
	$sql = "SELECT * FROM {$db_prefix}characters WHERE chPlayerID = $pid";
	$result = ba_db_query ($link, $sql);
	$row = ba_db_fetch_assoc ($result);
	$sNotes = $row ['chNotes'];
	$sOSP = $row ['chOSP'];

	//Check if all required info is present
	$IC_OK = True;
	if ($row ['chName'] == '')
		$IC_OK = False;
	?>
	<p>
	<table><tr>
	<td>Character Name:</td>
	<td><?php echo htmlentities (stripslashes ($row ['chName']))?></td>
	</tr><tr>
	<td>Race:</td>
	<td><?php echo htmlentities (stripslashes ($row ['chRace']))?></td>
	</tr><tr>
	<td>Culture:</td>
	<td><?php echo htmlentities (stripslashes ($row ['chCulture']))?></td>
	</tr><tr>
	<td>Reason for attending, and other IC notes:</td>
	<td><?php echo htmlentities (stripslashes ($row ['chReason']))?></td>
	</tr><tr>
	<td colspan = "2">&nbsp;</td></tr>
	<?php
	echo "<tr><td class = 'mid'><a href = 'admin_edit_ic.php?pid=$pid'>Edit IC Details</a></td>\n";
	if ($IC_OK && $IC_Confirmed == '') {
		echo "<td class = 'mid'>\n";
		echo "<form action = 'admin_viewdetails.php' method = 'get'>\n";
		echo "<input type = 'hidden' value = '$pid' name = 'pid'>\n";
		echo "<input type = 'submit' value = 'Confirm IC Details' name = 'btnSubmitIC'>\n</form></td>\n";
	}
	elseif ($IC_Confirmed == '')
		echo "<td><span class = 'sans-warn'>Cannot confirm - required information missing</span></td>\n";
	else
		echo "<td><span class = 'sans-green'>IC details confirmed $IC_Confirmed</span></td>\n";
	echo "</tr>\n";

	echo "</table>\n";
}
?>
</p>


<?php
include ('../inc/inc_foot.php');
?>
